﻿
Namespace PL.CoreSDK
    Public Class ListValues_PL
	Inherits PLBase

		Public Sub New()
			MyBase.New()
		End Sub
	
		Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
		End Sub
	
		Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating
	
		End Sub

		Public Overrides Function Delete() As Boolean
		   'Throw New NotImplementedException("Delete To Come...")
		   Return False
		End Function

		Public Overrides Function Validate() As Boolean
		   'Throw New NotImplementedException("Validate To Come...")
		   Return True
        End Function

        Public Shared Function ExportListValuesGCCTS(ByVal filename As String, ByVal listValueID As Guid) As Boolean
            Using ms As ListValues_PL = New ListValues_PL("SecurityGCCTSListValues")
                Try
                    ms.MetaDataTable.GetMetaCommand()
                    ms.MetaDataTable.MetaCommand.SQLWhere = String.Format("ListValueID = '{0}'", listValueID)
                    ms.InitDataForExport()
                    ms.Dataset.DataSetName = listValueID.ToString
                    ms.Dataset.WriteXml(filename, XmlWriteMode.WriteSchema)
                    Return True
                Catch ex As Exception
                    Throw ex
                End Try
                Return False
            End Using
        End Function

        Public Shared Function ExportListValuesGCCTSToMaster(ByVal listValueID As Guid) As Boolean
            Dim IsSaved As Boolean
            Using mcSource As ListValues_PL = New ListValues_PL("SecurityGCCTSListValues")
                Try
                    mcSource.MetaDataTable.GetMetaCommand()
                    mcSource.MetaDataTable.MetaCommand.SQLWhere = String.Format("ListValueID = '{0}'", listValueID)
                    mcSource.InitDataForExport()
                    mcSource.Dataset.DataSetName = listValueID.ToString
                    For Each oTable As DataTable In mcSource.Dataset.Tables
                        For Each oRow As DataRow In oTable.Rows
                            oRow.SetAdded()
                        Next
                    Next

                    'Bypass pour ne plus utiliser la connection par défaut
                    AppManager.IsConnected = False
                    AppManager.OverrideByPassConnectionStringTOMaster = True

                    'Indique qu'elle connection utiliser dans le App.Config
                    AppManager.ConnectionConfigName = "ConnectionToMyMaster"
                    Using mcDest As ListValues_PL = New ListValues_PL("SecurityGCCTSListValues")
                        mcDest.InitData()
                        mcDest.Dataset.Merge(mcSource.Dataset)
                        IsSaved = mcDest.Save()
                    End Using
                    Return IsSaved
                Catch ex As Exception
                    Throw ex
                Finally

                    'Remet en route la connection par défaut
                    AppManager.IsConnected = True
                    AppManager.OverrideByPassConnectionStringTOMaster = False

                    'Change pour utiliser la connection par défaut
                    AppManager.ConnectionConfigName = Nothing
                End Try
                Return False
            End Using
        End Function

        Public Shared Function ImportListValuesGCCTS(ByVal fileName As String) As Boolean
            Using dset As DataSet = New DataSet()
                dset.ReadXml(fileName, XmlReadMode.ReadSchema)
                Using ms As ListValues_PL = New ListValues_PL("SecurityGCCTSListValues")
                    'Hack: DataSet must be fill to have adaptercommand and merge...
                    ms.InitData()
                    ms.Dataset.Merge(dset)
                    Return ms.Save()
                End Using
            End Using
        End Function

        Public Function InitDataForExport() As Boolean
            Dim metaDataTableChild As CommonExt.MetaDataTable
            Dim aChildRelationField As String
            Dim parentGuid As Guid = Guid.Empty
            Try
                Me.MetaDataTable.FillBusinessApplicationData()
                Me.DataSet.Tables.Add(Me.MetaDataTable)
                'ici on veux un record seulement
                If Not String.IsNullOrEmpty(Me.MetaDataTable.MetaCommand.SQLWhere) Then
                    parentGuid = CType(Me.MetaDataTable.Rows(0).Item(0), Guid)
                End If
                Dim aParentRelationField As String = Me.MetaDataTable.MetaCommand.GUIDField
                For Each row As DataRow In Me.MetaDataTable.MetaCommand.GetChildDisplayQuery.Rows
                    metaDataTableChild = New CommonExt.MetaDataTable() With {.QueryName = row("QueryName").ToString}
                    aChildRelationField = Me.MetaDataTable.MetaCommand.GUIDField & "_FK"
                    '
                    metaDataTableChild.GetMetaCommand()
                    '
                    If parentGuid <> Guid.Empty Then
                        metaDataTableChild.MetaCommand.SQLWhere = String.Format("{0} = '{1}'", aChildRelationField, parentGuid)
                    End If
                    '
                    metaDataTableChild.FillBusinessApplicationData()
                    '
                    Me.DataSet.Tables.Add(metaDataTableChild)
                    Dim aDR As New DataRelation(row("Relation").ToString, Me.MetaDataTable.Columns(aParentRelationField), metaDataTableChild.Columns(aChildRelationField))
                    Me.DataSet.Relations.Add(aDR)
                Next
                Return True
            Catch ex As Exception
                Throw
            End Try
            Return False
        End Function

        Public Shared Function GetListValueByID(ByVal id As Guid) As DataRow
            Dim aRow As DataRow = Nothing
            Using lv As MetaQuery = MetaQuery.GetMetaQueryByName("LISTVALUES")
                lv.SQLWhere = String.Format("ListValueID = '{0}'", id.ToString)
                lv.FillResultQuery()
                If lv.ResultQuery.Rows.Count > 0 Then
                    aRow = lv.ResultQuery.Rows(0)
                End If
                Return aRow
            End Using
        End Function
	End Class
	End Namespace
